package com.ruoyi.fztz.lstb.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fztz.lstb.mapper.LstbMapper;
import com.ruoyi.fztz.lstb.domain.Lstb;
import com.ruoyi.fztz.lstb.service.ILstbService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

/**
 * 流水填报Service业务层处理
 *
 * @author huacai
 * @date 2024-11-18
 */
@Service
public class LstbServiceImpl implements ILstbService
{
    @Autowired
    private LstbMapper lstbMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询流水填报
     *
     * @param lstbId 流水填报主键
     * @return 流水填报
     */
    @Override
    public Lstb selectLstbByLstbId(String lstbId)
    {
        return lstbMapper.selectLstbByLstbId(lstbId);
    }

    /**
     * 查询流水填报列表
     *
     * @param lstb 流水填报
     * @return 流水填报
     */
    @Override
    public List<Lstb> selectLstbList(Lstb lstb)
    {
        return lstbMapper.selectLstbList(lstb);
    }

    /**
     * 新增流水填报
     *
     * @param lstb 流水填报
     * @return 结果
     */
    @Override
    public int insertLstb(Lstb lstb)
    {
        lstb.setCreateTime(DateUtils.getNowDate());
        return lstbMapper.insertLstb(lstb);
    }

    /**
     * 批量新增流水填报
     *
     * @param lstbs 流水填报List
     * @return 结果
     */
    @Override
    public int batchInsertLstb(List<Lstb> lstbs)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(lstbs)) {
            try {
                for (int i = 0; i < lstbs.size(); i++) {
                    int row = lstbMapper.insertLstb(lstbs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == lstbs.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改流水填报
     *
     * @param lstb 流水填报
     * @return 结果
     */
    @Override
    public int updateLstb(Lstb lstb)
    {
        lstb.setUpdateTime(DateUtils.getNowDate());
        return lstbMapper.updateLstb(lstb);
    }

    /**
     * 批量删除流水填报
     *
     * @param lstbIds 需要删除的流水填报主键
     * @return 结果
     */
    @Override
    public int deleteLstbByLstbIds(String[] lstbIds)
    {
        return lstbMapper.deleteLstbByLstbIds(lstbIds);
    }

    /**
     * 删除流水填报信息
     *
     * @param lstbId 流水填报主键
     * @return 结果
     */
    @Override
    public int deleteLstbByLstbId(String lstbId)
    {
        return lstbMapper.deleteLstbByLstbId(lstbId);
    }
}
